﻿using GameControler.Emgu;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using JilyHelper;
using System.Drawing;
using PlatformCommon.Manager;

namespace GameControler.Game
{
    public class FightingView : ViewObject
    {

        public event EventHandler EndingFighting;

        public override string FloderName
        {
            get { return "FightingView"; }
        }

        private static FightingView instance;
        public static FightingView Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FightingView();
                }
                return instance;
            }
        }

        protected FightingView()
            : base()
        {

        }

        protected override void InitTemplates()
        {
            base.InitTemplates();
        }

        public override bool IsCurrentView(System.Drawing.Bitmap intputbmp)
        {

            if (MoveView.Instance.IsCurrentView(intputbmp))
            {
                GameShow.GameHandle.SendMove(GameHelper.RightButton, 1000);
                return true;
            }

            Point? autopoint = null;
            var width = intputbmp.Width;
            var height = intputbmp.Height;
            foreach (var item in this.Templates)
            {
                var x = item.Point.X > 0 ? 0 : intputbmp.Width + item.Point.X;
                var y = item.Point.Y > 0 ? 0 : intputbmp.Height + item.Point.Y;
                var clipbitmap = intputbmp.Clone(new Rectangle(x, y, Math.Abs(item.Point.X), Math.Abs(item.Point.Y)), intputbmp.PixelFormat);
                var md = JilyEmgu.MatchAllResultRGB(clipbitmap, item, 1d);
                //clipbitmap.Save(@"c:\11.png");
                var matchhsv = JilyEmgu.MatchResult(clipbitmap, item, 0.5);

                clipbitmap.Dispose();
                if ((item.Team != "0") != (md.IsMatch || matchhsv.IsMatch))
                {
                    return false;
                }
                if (item.Team.ToLower() == "auto")
                {
                    if (md.IsMatch)
                    {
                        autopoint = new Point(md.Point.X - item.Point.X + 40, md.Point.Y - item.Point.Y + 40);
                    }
                    else if (matchhsv.IsMatch)
                    {
                        autopoint = new Point(matchhsv.Point.X - item.Point.X + 40, matchhsv.Point.Y - item.Point.Y + 40);
                    }
                }
            }

            var auto = AutoFightingView.Instance.IsCurrentView(intputbmp);
            if (!auto)
            {
                if (autopoint != null)
                {
                    Log4Wrap.Logger.Info("Fighting界面匹配" + "___" + "开启自动战斗");

                    GameShow.GameHandle.SendClick(autopoint.Value);
                }

                //if (MoveView.Instance.IsCurrentView(intputbmp))
                //{
                //    GameShow.GameHandle.SendMove(GameHelper.RightButton, 1000);
                //}
                //else
                {

                }
            }
            AutoHit();

            return true;
        }

        #region 战斗

        int hitcount = 0;
        bool isleft = true;
        private void AutoHit()
        {
            hitcount++;

            if (hitcount < 5)
            {
                return;
            }
            Log4Wrap.Logger.Info("Fighting界面匹配" + "___" + "手动战斗" + "___" + "1030, 571");

            GameShow.GameHandle.SendClick(GameHelper.HitDefaultButton);

            if (hitcount >= 20)
            {
                if (isleft)
                {
                    Log4Wrap.Logger.Info("Fighting界面匹配" + "___" + "手动战斗" + "___" + "右移动");
                    GameShow.GameHandle.SendMove(GameHelper.RightButton, 2500);
                }
                else
                {
                    Log4Wrap.Logger.Info("Fighting界面匹配" + "___" + "手动战斗" + "___" + "左移动");
                    GameShow.GameHandle.SendMove(GameHelper.LeftButton, 500);
                }
                isleft = !isleft;

                hitcount = 0;

                return;
            }

            if (hitcount == 5 || hitcount == 8 || hitcount == 12 || hitcount == 15 || hitcount == 18)
            {
                Log4Wrap.Logger.Info("Fighting界面匹配" + "___" + "手动战斗" + "___" + "使用技能");
                GameShow.GameHandle.SendClick(GameHelper.Hit1Button);
                GameShow.GameHandle.SendClick(GameHelper.Hit2Button);
                GameShow.GameHandle.SendClick(GameHelper.Hit3Button);
                GameShow.GameHandle.SendClick(GameHelper.Hit4Button);
            }
        }


        #endregion

    }

}
